package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingPlanProMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:
 * @Description: TODO
 * @DateTime: 2024/6/11 9:45
 **/
@Service
@Transactional
public class NursingPlanSerciveImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingPlanProMapper nursingPlanProMapper;


    @Override
    public List<NursingPlanVo> queryList() {
        return nursingPlanMapper.selectList();
    }

    @Override
    public void addNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(dto,nursingPlan);
        //新增护理计划
        nursingPlanMapper.insert(nursingPlan);
        //获取新计划id
        Long id1234 = nursingPlan.getId();
        //获取新增的 中间关联表
        List<NursingProPlan> NPPList = dto.getProjectPlans();

        if(NPPList!=null && NPPList.size()>0){
            for (NursingProPlan proPlan : NPPList) {
                proPlan.setPlanId(id1234);
            }
            //添加关联表
            nursingPlanProMapper.insertNursingProPlan(NPPList);
        }
    }

    @Override
    public PageResponse findPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);

        Page<NursingPlanVo> page = nursingPlanMapper.selectPage(name,status);
        //如果该计划下存在项目，count>0不能删除，置灰
        //将 page集合 转成 list集合
        List<NursingPlanVo> list = page.getResult();
        // 从上述集合中获取集合中每个对象的ID集合
        List<Long> collect = list.stream().map(a
                -> a.getId()).collect(Collectors.toList());
        //获取每个项目id被使用的次数
        List<Map> mapList = nursingPlanMapper.countFind(collect);
        //toMap两个参数，一个参数是key, 一个参数是value
        Map<Long, Long> map = mapList.stream().collect(Collectors.toMap(
                mp->Long.valueOf(mp.get("pid").toString()),
                mp->Long.valueOf(mp.get("ct").toString()))
        );
        list.stream().forEach(vo->
                vo.setCount(map.getOrDefault(vo.getId(),0L)));

        return PageResponse.of(page,NursingPlanVo.class);
    }

    @Override
    public NursingPlanVo findById(Long id) {
        //查询
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        if(nursingPlan == null){
            throw new BaseException(BasicEnum.PLAN_SELECT_FAIL);
        }
        //根据员工id查询关联信息
        List<NursingProPlan> list = nursingPlanProMapper.selectList(id);
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtil.copyProperties(nursingPlan,vo);
        //将关联信息加入集合
        vo.setProjectPlans(list);
        return vo;
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }

    @Override
    public void deletePlan(Long id) {
        nursingPlanMapper.deletePlan(id);
    }

    @Override
    public void updatenursingPlan(NursingPlanDto dto) {
        if(dto == null){
            throw new BaseException(BasicEnum.PLAN_UPDATE_FAIL);
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(dto,nursingPlan);
        nursingPlanMapper.update(nursingPlan);

        NursingPlanVo vo = new NursingPlanVo();
        BeanUtil.copyProperties(nursingPlan,vo);

        Long planId = nursingPlan.getId();
        List<Long> planIds = List.of(planId);
        //2.根据id删除旧的关联表
        nursingPlanProMapper.deleteNPP(planIds);
        //3.添加新的NPP
        List<NursingProPlan> npp = dto.getProjectPlans();
        if(npp != null && npp.size()>0){
            for (NursingProPlan nursingProPlan : npp) {
                nursingProPlan.setPlanId(planId);
            }
            //添加
            nursingPlanProMapper.insertNursingProPlan(npp);
        }
    }
}
